﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hotel.Domain;
using HOTEL.Dao;
using Hotel.Util;
using HAINGUYEN_HOTEL.Helper;

namespace HAINGUYEN_HOTEL
{
    public partial class RoomAvailable : System.Web.UI.Page
    {
        private PhongDao _phongDao = new PhongDao();
        private HangPhongDao _hangphongDao = new HangPhongDao();
        private LoaiPhongDao _loaiphongDao = new LoaiPhongDao();
        protected DateTime? TuNgay
        {
            get
            {
                return ConvertUtil.ToDateTime(Request.QueryString[Constans.SearchTuNgay]);
            }
            set
            {
                txtTuNgay.Text = Request.QueryString[Constans.SearchTuNgay];
            }
        }
        protected DateTime? DenNgay
        {
            get
            {
                return ConvertUtil.ToDateTime(Request.QueryString[Constans.SearchDeNgay]);
            }
            set
            {
                txtDenNgay.Text = Request.QueryString[Constans.SearchDeNgay];
            }
        }
        protected int? SoPhong
        {
            get
            {
                return ConvertUtil.ToInteger(Request.QueryString[Constans.SearchRoom]);
            }
            set
            {
                txtSoPhong.Text = Request.QueryString[Constans.SearchRoom];
            }
        }
        protected int? SoNguoi
        {
            get
            {
                return ConvertUtil.ToInteger(Request.QueryString[Constans.SearchPerson]);
            }
            set
            {
                txtSoNguoi.Text = Request.QueryString[Constans.SearchPerson];
            }
        }
        protected int? LoaiPhong
        {
            get
            {
                return ConvertUtil.ToInteger(Request.QueryString[Constans.SearchLoaiPhong]);
            }
            set
            {
                var item = ddlLoaiphong.Items.FindByValue(Request.QueryString[Constans.SearchLoaiPhong]);
                if (item != null)
                {
                    ddlLoaiphong.SelectedIndex = ddlLoaiphong.Items.IndexOf(item);
                }
            }
        }
        protected int? HangPhong
        {
            get
            {
                return ConvertUtil.ToInteger(Request.QueryString[Constans.SearchClassRoom]);
            }
            set
            {
                var item = ddlHangphong.Items.FindByValue(Request.QueryString[Constans.SearchClassRoom]);
                if (item != null)
                {
                    ddlHangphong.SelectedIndex = ddlHangphong.Items.IndexOf(item);
                }
            }
        }
        protected decimal? Gia
        {
            get
            {
                return ConvertUtil.ToInteger(Request.QueryString[Constans.SearchPrice]);
            }
            set
            {
                txtGia.Text = Request.QueryString[Constans.SearchPrice];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadReference();
                BindToList();

                Object objsonguoi = Session["songuoi"];
                if (objsonguoi != null)
                {
                    this.txtSoNguoi.Text = Session["songuoi"] as string;
                }
                Object objsophong = Session["sophong"];
                if (objsophong != null)
                {
                    this.txtSoPhong.Text = Session["sophong"] as string;
                }
               
            }
            
        }
        protected void LoadReference()
        {
            Helper.Helper.BindDropDownList(ddlHangphong, _hangphongDao.GetList(), "TenHangPhong", "MaHangPhong", true);
            Helper.Helper.BindDropDownList(ddlLoaiphong, _loaiphongDao.GetList(), "TenLoaiPhong", "MaLoaiPhong", true);
            TuNgay = null;
            DenNgay = null;
            SoPhong = null;
            SoNguoi = null;
            LoaiPhong = null;
            HangPhong = null;
            Gia = null;
        }
        protected void BindToList()
        {
            var list = _phongDao.GetHangPhongAvalilable(TuNgay, DenNgay, SoNguoi, LoaiPhong, HangPhong, SoPhong, Gia);
            rpHangPhong.DataSource = list;
            rpHangPhong.DataBind();
            updtlist.Update();
        }
        protected void btnOrder_Click(object sender, EventArgs e)
        {
            var obj = (Button)sender;
            if (obj != null)
            {
                var number = 1;
                var data = obj.CommandArgument.ToString();
                var iddata = data.Split('-');
                if (Session[SessionKey.MyRoom] == null)
                {
                    var bangia=_phongDao.GetBangGiaPhong( int.Parse(iddata[1]),int.Parse(iddata[0]));
                    if(bangia!=null){
                        MyRoom item = new MyRoom
                        {
                            TenHangPhong = bangia.HANG_PHONG.TenHangPhong,
                            TenLoaiPhong = bangia.LOAI_PHONG.TenLoaiPhong,
                            MaHangPhong =bangia.MaHangPhong,
                            MaLoaiPhong = bangia.MaLoaiPhong,
                            SoLuong=1,
                            SoNguoi = int.Parse(iddata[2]) * number,
                            Gia=bangia.Gia.Value
                        };
                        List<MyRoom> objlist=new List<MyRoom>();
                        objlist.Add(item);
                        Session[SessionKey.MyRoom]=objlist;
                    }
                }
                else
                {
                    var listroom=Session[SessionKey.MyRoom] as List<MyRoom>;
                    var bangia=_phongDao.GetBangGiaPhong( int.Parse(iddata[1]),int.Parse(iddata[0]));
                    if(bangia!=null){
                        MyRoom item = new MyRoom
                        {
                            TenHangPhong=bangia.HANG_PHONG.TenHangPhong,
                            TenLoaiPhong=bangia.LOAI_PHONG.TenLoaiPhong,
                            MaHangPhong =bangia.MaHangPhong,
                            MaLoaiPhong = bangia.MaLoaiPhong,
                            SoLuong=1,
                            SoNguoi = int.Parse(iddata[2]) * number,
                            Gia=bangia.Gia.Value
                        };
                        bool flag = true;
                        for(int i=0;i<listroom.Count;i++)
                        {
                            if (listroom[i].MaHangPhong.Value.Equals(item.MaHangPhong) && listroom[i].MaLoaiPhong.Value.Equals(item.MaLoaiPhong))
                            {
                                listroom[i].SoLuong = listroom[i].SoLuong + item.SoLuong;
                                listroom[i].SoNguoi = int.Parse(iddata[2]) * listroom[i].SoLuong;
                                flag = false;
                                break;
                            }
                        }
                        if (flag)
                        {
                            listroom.Add(item);
                        }
                        Session[SessionKey.MyRoom]=listroom;
                    }
                }
                System.Threading.Thread.Sleep(2000); 
                Response.Redirect(Request.RawUrl, true);
                
            }
        }
        protected void RebindMyRoom()
        {
            var upStatus = Master.FindControl("upStatus") as UpdatePanel;
            var lbPersonNumber = Master.FindControl("lbPersonNumber") as Label;
            var lbRoomNumber = Master.FindControl("lbRoomNumber") as Label;
            if (Session[SessionKey.MyRoom] != null)
            {
                var list = Session[SessionKey.MyRoom] as List<MyRoom>;
                lbPersonNumber.Text = list.Sum(p => p.SoNguoi).Value.ToString("D2");
                lbRoomNumber.Text = list.Sum(p => p.SoLuong).Value.ToString("D2");
                upStatus.Update();
            }
            else
            {
                lbPersonNumber.Text = "0";
                lbRoomNumber.Text = "0";
                upStatus.Update();
            }
        }
        protected void btn_Submit_Click1(object sender, EventArgs e)
        {
            string str = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=conghai02@gmail.com";
            str += "&item_name=Lec ognf ha";
            str += "&currency_code=USD";
            str += "&amount= 25.00";
            str += "&quantity= 1";
            str += "&shipping=10";
            Response.Redirect(str, true);

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.txtSoPhong.Text.CompareTo("") > 0)
            {
                Session["songuoi"] = txtSoNguoi.Text;
            }
            if (this.txtRoomNumber.Text.CompareTo("") > 0)
            {
                Session["sophong"] = txtRoomNumber.Text;
            }
            if (this.txtTuNgay.Text.CompareTo("") > 0)
            {
                Session["from"] = txtTuNgay.Text;
            }
            if (this.txtDenNgay.Text.CompareTo("") > 0)
            {
                Session["to"] = txtDenNgay.Text;
            }
            var list = _phongDao.GetHangPhongAvalilable(ConvertUtil.ToDateTime(txtTuNgay.Text.Trim()),ConvertUtil.ToDateTime(txtDenNgay.Text.Trim()),
                ConvertUtil.ToInteger(txtSoNguoi.Text.Trim()), ConvertUtil.ToInteger(ddlLoaiphong.SelectedValue), ConvertUtil.ToInteger(ddlHangphong.SelectedValue), ConvertUtil.ToInteger(txtSoPhong.Text.Trim()),ConvertUtil.ToInteger(txtGia.Text.Trim()));
            rpHangPhong.DataSource = list;
            rpHangPhong.DataBind();
            updtlist.Update();
        }
        protected void rpHangPhong_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (!e.Item.ItemType.Equals(ListItemType.Item) &&
               !e.Item.ItemType.Equals(ListItemType.AlternatingItem)) return;
            var dlloaiphong = e.Item.FindControl("datalistPhong") as DataList;
            var upListphong = e.Item.FindControl("uplistPhong") as UpdatePanel;

            if (dlloaiphong != null)
            {
                var ListHangPhong = rpHangPhong.DataSource as List<HANG_PHONG>;
                if (ListHangPhong != null)
                {
                    var hangphongitem = ListHangPhong[e.Item.ItemIndex];
                    if (hangphongitem != null)
                    {
                        var listLoaiPhong = _phongDao.GetLoaiPhongAvalilable(hangphongitem.MaHangPhong, TuNgay, DenNgay, SoNguoi, LoaiPhong, HangPhong, SoPhong, Gia);
                        var banggialist = new List<BANG_GIA_PHONG>();
                        foreach (var loaiphong in listLoaiPhong)
                        {
                            var banggia = _phongDao.GetBangGiaPhong(hangphongitem.MaHangPhong, loaiphong.MaLoaiPhong);
                            if (banggia != null)
                                banggialist.Add(banggia);
                        }
                        if (banggialist.Count != 0)
                        {
                            dlloaiphong.ItemDataBound += new DataListItemEventHandler(dlloaiphong_ItemDataBound);
                            dlloaiphong.DataSource = banggialist;
                            dlloaiphong.DataBind();
                            upListphong.Update();
                        }
                        else
                        {
                            upListphong.Visible = false;
                            upListphong.Update();
                        }
                    }
                }
            }
        }

        protected void dlloaiphong_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (!e.Item.ItemType.Equals(ListItemType.Item) &&
               !e.Item.ItemType.Equals(ListItemType.AlternatingItem)) return;
            var lbcount = e.Item.FindControl("lbCount") as Label;
            var datalist = (DataList)sender;
            var bangialist = datalist.DataSource as List<BANG_GIA_PHONG>;
            if (bangialist != null)
            {
                var banggiaitem=bangialist[e.Item.ItemIndex];
                var countvalue = _phongDao.CountPhongAvalilable(banggiaitem.MaHangPhong, banggiaitem.MaLoaiPhong);
                lbcount.Text = countvalue.ToString();
            }
        }
        
    }
}